﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersonWithEvent
{
    class Person : IPropertyChangeNotification
    {
        private string firstName;
        private string lastName;
        private byte age;
        private DateTime birthDate;

        private event PropertyChangedEventHandler propertyChanged;
        
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                string oldValue = this.FirstName;
                firstName = value;
                OnPropertyChanged("FirstName", oldValue, value);
            }
        }

        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                string oldValue = this.LastName;
                lastName = value;
                OnPropertyChanged("LastName", oldValue, value);
            }
        }

        public byte Age
        {
            get
            {
                return age;
            }
            set
            {
                byte oldValue = this.Age;
                age = value;
                OnPropertyChanged("Age", oldValue, value);
            }
        }

        public DateTime BirthDate
        {
            get
            {
                return birthDate;
            }
            set
            {
                DateTime oldValue = this.BirthDate;
                birthDate = value;
                OnPropertyChanged("BirthDate", oldValue, value);
            }
        }
        
        public Person(string firstName, string lastname, byte age, DateTime birthDate)
        {
            this.firstName = firstName;
            this.lastName = lastname;
            this.age = age;
            this.birthDate = birthDate;
        }

        protected void OnPropertyChanged(string property, Object oldValue, Object newValue)
        {
            if (propertyChanged != null)
            {
                PropertyChangedEventArgs eventArgs = new PropertyChangedEventArgs(property, oldValue, newValue);
                propertyChanged(this, null);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged
        {
            add 
            { 
                propertyChanged += value; 
            }
            remove 
            {
                propertyChanged -= value;
            }
        }
    }
}
